<?php
namespace Admin\Controller;
use Think\Controller;
class BaseController extends Controller {
    public function _initialize()
    {
        $role_id = intval(I('role_id')) == 0 ? 1 : intval(I('role_id')) ;//组id,不传默认为超级管理员
        $path = 'admin';//默认后台名称，这个与数据库表node顶级节点有关
        $model = strtolower(CONTROLLER_NAME);//这里获取模块方法，需要转成小写
        $action = strtolower(ACTION_NAME);//这里获取控制器方法，需要转成小写
        if(empty($model) OR empty($action))
        {
            die('模块或者方法不存在');
        }
        $access_arr = array();
        if($role_id == 1)
        {
            $access_arr = M('node')->select();//超级管理员获取所有权限节点
        }
        else
        {
            //根据不同组来获取节点
            $access_arr = M('role')->where(array('role.id'=>$role_id),'role.status'=>1)->join(array('left join access on access.role_id = role.id','left join node on access.node_id = node.id and access.pid = node.pid'))->field('node.*')->select();
        }
        if(empty($access_arr))
        {
            die('你没有权限');
        }
        $number = 0;
        foreach($access_arr as $key=>$val)
        {
            //后台入口权限
            if ($val['model'] === $path && $val['action'] === $path && intval($val['level']) === 1 ) 
            {
                $number ++;
            }
            //model权限
            if ($val['model'] === $model && $val['action'] === $model && intval($val['level']) === 2)   
                $number ++;
            //action权限
            if ($val['model'] === $model && $val['action'] === $action && intval($val['level']) === 3)  
                $number ++;
        }
        if($number !== 3)
        {
            die('你没有权限等级不够');
        }
    }
}